﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SVC.client;
namespace UI.Forms
{
    public partial class goodsclass_N : DevExpress.XtraEditors.XtraForm
    {
        public delegate void DoAction();//如果有参数的话，定义参数 
        int count =0;
        public DoAction FatherRefresh;
        public goodsclass_N()
        {
            InitializeComponent();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {

        }

        private void bindTreelist()
        {
            treeList1.DataSource = my.bw.GoodsClass_Get("ClassID", my.ACID);
            treeList1.ExpandAll();
        }

        private void btn_newclose_Click(object sender, EventArgs e)
        {

        }

        private void goodsclass_N_Load(object sender, EventArgs e)
        {
            bindTreelist();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            GoodsClass PGC = treeList1.GetDataRecordByNode(treeList1.FocusedNode) as GoodsClass;
            if (PGC == null) return;
            popupContainerEdit1.EditValue = string.Format("{0},{1}", PGC.ClassID,PGC.ClassName);
            popupContainerEdit1.ClosePopup();

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            popupContainerEdit1.Text ="";
            popupContainerEdit1.ClosePopup();
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!dxValidationProvider1.Validate()) return;
            GoodsClass gc = new GoodsClass();
            gc.ClassID = textEdit2.Text.Trim();
            gc.ClassName = textEdit1.Text.Trim();
            if (!string.IsNullOrWhiteSpace(popupContainerEdit1.Text))
            {
                gc.ParentID = popupContainerEdit1.Text.Split(',')[0];
            }
            string result = my.bw.GoodsClass_Add(gc, my.ACID);
            if (string.IsNullOrWhiteSpace(result))
            {
                textEdit1.Text = string.Empty;
                textEdit2.Text = string.Empty;
                treeList1.EditingValue = null;
                bindTreelist();
                count++;
            }
            else
            {
                Comm.ShowErorr(result);
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!dxValidationProvider1.Validate()) return;
            GoodsClass gc = new GoodsClass();
            gc.ClassID = textEdit2.Text.Trim();
            gc.ClassName = textEdit1.Text.Trim();
            if (!string.IsNullOrWhiteSpace(popupContainerEdit1.Text))
            {
                gc.ParentID = popupContainerEdit1.Text.Split(',')[0];
            }

            string result = my.bw.GoodsClass_Add(gc, my.ACID);
            if (string.IsNullOrWhiteSpace(result))
            {
                count++;
                this.Close();
            }
            else
            {
                Comm.ShowErorr(result);
            }
        }

        private void goodsclass_N_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (count>0)
            {
                FatherRefresh();
            }
        }
    }
}